<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">

    <xsl:variable name="display.chaptertoc.as.itemizedlist" select="false()"/>

    <xsl:template name="toc.line">
    
        <xsl:variable name="id">
            <xsl:call-template name="object.id" />
        </xsl:variable>
        
        <xsl:variable name="label">
            <xsl:if
                test="self::part or
                self::chapter or
                self::article or
                self::appendix
                or self::preface or
                self::glossary or
                self::index">
                <xsl:call-template name="gentext">
                    <xsl:with-param name="key">
                        <xsl:choose>
                            <xsl:when test="self::part">Part</xsl:when>
                            <xsl:when test="self::chapter">Chapter</xsl:when>
                            <xsl:when test="self::article">Chapter</xsl:when>
                            <xsl:when test="self::preface">Preface</xsl:when>
                            <xsl:when test="self::glossary">Glossary</xsl:when>
                            <xsl:when test="self::index">Index</xsl:when>
                            <xsl:when test="self::appendix">Appendix</xsl:when>
                        </xsl:choose>
                    </xsl:with-param>
                </xsl:call-template>
                <xsl:text> </xsl:text>
            </xsl:if>
            
            <xsl:if test="self::part
                    or self::chapter
                    or self::article
                    or self::appendix">
                <xsl:apply-templates select="." mode="label.markup" />
            </xsl:if>
            
        </xsl:variable>

        <fo:block text-align-last="justify" end-indent="{$toc.indent.width}pt"
            last-line-end-indent="-{$toc.indent.width}pt">
            <xsl:if
                test="self::part or
                self::chapter or
                self::article or
                self::appendix or
                self::preface or
                self::glossary or
                self::index">
                <xsl:attribute name="font-weight">
                    <xsl:text>bold</xsl:text>
                </xsl:attribute>
                <xsl:attribute name="font-size">
                    <xsl:text>1.2em</xsl:text>
                </xsl:attribute>
                <xsl:attribute name="space-before">
                    <xsl:text>1em</xsl:text>
                </xsl:attribute>
                <xsl:attribute name="space-after">
                    <xsl:text>0.4em</xsl:text>
                </xsl:attribute>
                <xsl:attribute name="page-break-after">
                    <xsl:text>avoid</xsl:text>
                </xsl:attribute>
            </xsl:if>
            <fo:inline hyphenate="false" keep-with-next.within-line="always">
                <xsl:if
                    test="self::part or
                    self::chapter or
                    self::article or
                    self::appendix or
                    self::preface or
                    self::index or
                    self::glossary">
                    <xsl:attribute name="color">
                        <xsl:value-of select="$title.font.color" />
                    </xsl:attribute>
                </xsl:if>
                <fo:basic-link internal-destination="{$id}">
                    <xsl:if test="$label != ''">
                        <xsl:copy-of select="$label" />
                        
                        <xsl:if test="not(self::index)">
                            <xsl:value-of select="$autotoc.label.separator" />
                        </xsl:if>
                        
                    </xsl:if>
                    <xsl:if test="not(self::index)">
                        <!-- these are the elements that have a title -->
                        <xsl:apply-templates select="." mode="title.markup" />
                    </xsl:if>
                </fo:basic-link>
            </fo:inline>
            <fo:inline keep-together.within-line="always">
                <xsl:if
                    test="self::part or
                    self::chapter or
                    self::article or
                    self::appendix or
                    self::preface or
                    self::index or
                    self::glossary">
                    <xsl:attribute name="color">
                        <xsl:value-of select="$title.font.color" />
                    </xsl:attribute>
                </xsl:if>
                <xsl:text> </xsl:text>
                <xsl:element name="fo:leader">
                    <xsl:attribute name="leader-pattern-width">
                        <xsl:text>3pt</xsl:text>
                    </xsl:attribute>
                    <xsl:attribute name="leader-alignment">
                        <xsl:text>reference-area</xsl:text>
                    </xsl:attribute>
                    <xsl:attribute name="keep-with-next.within-line">
                        <xsl:text>always</xsl:text>
                    </xsl:attribute>
                    <xsl:attribute name="leader-pattern">
                        <xsl:choose>
                            <xsl:when
                                test="self::part or
                                self::chapter or
                                self::article or
                                self::appendix or
                                self::preface or
                                self::index or
                                self::glossary">
                                <xsl:text>space</xsl:text>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:text>dots</xsl:text>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                </xsl:element>
                <xsl:text> </xsl:text>
                <fo:basic-link internal-destination="{$id}">
                    <fo:page-number-citation ref-id="{$id}" />
                </fo:basic-link>
            </fo:inline>
        </fo:block>

    </xsl:template>

    <xsl:template name="component.toc">
        <xsl:param name="toc-context" select="." />
        <xsl:param name="toc.title.p" select="true()" />
        <xsl:variable name="id">
            <xsl:call-template name="object.id" />
        </xsl:variable>
        <xsl:variable name="cid">
            <xsl:call-template name="object.id">
                <xsl:with-param name="object" select="$toc-context" />
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="nodes"
            select="section|sect1|refentry|article|bibliography|glossary|appendix|index" />
        <xsl:if test="$nodes">
            <xsl:choose>
                <xsl:when test="(self::chapter or self::article) and $display.chaptertoc.as.itemizedlist">
                    <xsl:element name="fo:block">
                        <xsl:attribute name="id">
                            <xsl:value-of select="concat('toc...',$id)" />
                        </xsl:attribute>
                        <xsl:attribute name="use-attribute-sets">toc.margin.properties</xsl:attribute>
                        <xsl:if test="$toc.title.p">
                            <xsl:call-template name="table.of.contents.titlepage" />
                        </xsl:if>
                        <xsl:element name="fo:list-block">
                            <xsl:attribute name="provisional-distance-between-starts">0.5in</xsl:attribute>
                            <xsl:apply-templates select="$nodes" mode="itemized-toc">
                                <xsl:with-param name="toc-context" select="$toc-context" />
                            </xsl:apply-templates>
                        </xsl:element>
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                    <fo:block id="toc...{$id}" xsl:use-attribute-sets="toc.margin.properties">
                        <xsl:if test="$toc.title.p">
                            <xsl:call-template name="table.of.contents.titlepage" />
                        </xsl:if>
                        <xsl:apply-templates select="$nodes" mode="toc">
                            <xsl:with-param name="toc-context" select="$toc-context" />
                        </xsl:apply-templates>
                    </fo:block>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>

    <xsl:template match="section" mode="itemized-toc">
        <xsl:param name="toc-context" select="." />
        <xsl:variable name="id">
            <xsl:call-template name="object.id" />
        </xsl:variable>
        <xsl:variable name="cid">
            <xsl:call-template name="object.id">
                <xsl:with-param name="object" select="$toc-context" />
            </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="depth" select="count(ancestor::section) + 1" />
        <xsl:variable name="reldepth" select="count(ancestor::*)-count($toc-context/ancestor::*)" />
        <xsl:variable name="depth.from.context"
            select="count(ancestor::*)-count($toc-context/ancestor::*)" />

        <xsl:if test="$toc.section.depth &gt;= $depth">

            <xsl:call-template name="toc.list-item" />

            <!--xsl:if
                test="$toc.section.depth > $depth 
                and $toc.max.depth > $depth.from.context
                and section">
                <fo:block id="toc.{$cid}.{$id}">
                    <xsl:attribute name="margin-left">
                        <xsl:call-template name="set.toc.indent">
                            <xsl:with-param name="reldepth" select="$reldepth" />
                        </xsl:call-template>
                    </xsl:attribute>
                    <xsl:apply-templates select="section" mode="toc">
                        <xsl:with-param name="toc-context" select="$toc-context" />
                    </xsl:apply-templates>
                </fo:block>
            </xsl:if-->
        </xsl:if>

    </xsl:template>

    <xsl:template name="toc.list-item">
        <xsl:variable name="id">
            <xsl:call-template name="object.id" />
        </xsl:variable>
        <xsl:variable name="label">
            <xsl:if
                test="self::chapter or self::article or self::appendix or self::preface or self::glossary or self::index">
                <xsl:call-template name="gentext">
                    <xsl:with-param name="key">
                        <xsl:choose>
                            <xsl:when test="self::chapter">Chapter</xsl:when>
                            <xsl:when test="self::article">Chapter</xsl:when>
                            <xsl:when test="self::preface">Preface</xsl:when>
                            <xsl:when test="self::glossary">Glossary</xsl:when>
                            <xsl:when test="self::index">Index</xsl:when>
                            <xsl:when test="self::appendix">Appendix</xsl:when>
                        </xsl:choose>
                    </xsl:with-param>
                </xsl:call-template>
                <xsl:text> </xsl:text>
            </xsl:if>
            <xsl:choose>
                <xsl:when test="self::chapter or self::article or self::appendix">
                    <xsl:apply-templates select="." mode="label.markup" />
                </xsl:when>
                <xsl:otherwise />
            </xsl:choose>
        </xsl:variable>
        <xsl:element name="fo:block"><!-- 
        <text-align-last="justify" end-indent="{$toc.indent.width}pt"
            last-line-end-indent="-{$toc.indent.width}pt"> -->
            <xsl:if
                test="self::chapter or self::article or self::appendix or self::preface or self::glossary or self::index">
                <xsl:attribute name="font-weight">
                    <xsl:text>bold</xsl:text>
                </xsl:attribute>
                <xsl:attribute name="font-size">
                    <xsl:text>1.1em</xsl:text>
                </xsl:attribute>
                <xsl:attribute name="space-before">
                    <xsl:text>1em</xsl:text>
                </xsl:attribute>
                <xsl:attribute name="space-after">
                    <xsl:text>0.4em</xsl:text>
                </xsl:attribute>
            </xsl:if>
            <fo:inline keep-with-next.within-line="always">
                <xsl:if
                    test="self::chapter or self::article or self::appendix or self::preface or self::index or self::glossary">
                    <xsl:attribute name="color">
                        <xsl:value-of select="$title.font.color" />
                    </xsl:attribute>
                </xsl:if>
                <fo:basic-link internal-destination="{$id}">
                    <xsl:if test="$label != ''">
                        <xsl:copy-of select="$label" />
                        <xsl:value-of select="$autotoc.label.separator" />
                    </xsl:if>
                    <xsl:apply-templates select="." mode="title.markup" />
                </fo:basic-link>
            </fo:inline>
            <fo:inline keep-together.within-line="always">
                <xsl:if
                    test="self::chapter or self::article or self::appendix or self::preface or self::index or self::glossary">
                    <xsl:attribute name="color">
                        <xsl:value-of select="$title.font.color" />
                    </xsl:attribute>
                </xsl:if>
                <xsl:text> </xsl:text>
                <xsl:element name="fo:leader">
                    <xsl:attribute name="leader-pattern-width">
                        <xsl:text>3pt</xsl:text>
                    </xsl:attribute>
                    <xsl:attribute name="leader-alignment">
                        <xsl:text>reference-area</xsl:text>
                    </xsl:attribute>
                    <xsl:attribute name="keep-with-next.within-line">
                        <xsl:text>always</xsl:text>
                    </xsl:attribute>
                    <xsl:attribute name="leader-pattern">
                        <xsl:choose>
                            <xsl:when
                                test="self::chapter or self::article or self::appendix or self::preface or self::index or self::glossary">
                                <xsl:text>space</xsl:text>
                            </xsl:when>
                            <xsl:otherwise>
                                <xsl:text>dots</xsl:text>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                </xsl:element>
                <xsl:text> </xsl:text>
                <fo:basic-link internal-destination="{$id}">
                    <fo:page-number-citation ref-id="{$id}" />
                </fo:basic-link>
            </fo:inline>
        </xsl:element>
        
    </xsl:template>
    
</xsl:stylesheet>
